package com.social.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.social.entity.*;
import com.social.service.certificate.CertificateService;
import com.social.service.collect.CollectService;
import com.social.service.emergencyContact.EmergencyContactService;
import com.social.service.houseKeeper.HouseKeeperService;
import com.social.service.houseKeeperMy.HouseKeeperMyService;
import com.social.service.trainExperience.TrainExperienceService;
import com.social.service.workexPerience.WorkexPerienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.social.common.Tool.getIdByUUId;
import static com.social.common.Tool.isNotNull;
/*
* 设置收藏、推荐等控制层
* */
@Controller
@RequestMapping(value = "/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private HouseKeeperService houseKeeperService;
    @Autowired
    private HouseKeeperMyService houseKeeperMyService;
    @Autowired
    private WorkexPerienceService workexPerienceService;
    @Autowired
    private TrainExperienceService trainExperienceService;
    @Autowired
    private EmergencyContactService emergencyContactService;
    @Autowired
    private CertificateService certificateService;
    @ResponseBody
    @RequestMapping(value = "/add")//收藏添加
    public String add(String houseKeeperId,String state, HttpSession session){
        JSONObject result = new JSONObject();
        HousekeeperCollect housekeeperCollect = new HousekeeperCollect();
        AdminUser adminuser=(AdminUser)session.getAttribute("adminuser");
        housekeeperCollect.setId(getIdByUUId());
        housekeeperCollect.setAdminuserid(adminuser.getId());
        housekeeperCollect.setHousekeeperid(houseKeeperId);
        housekeeperCollect.setState(state);
        housekeeperCollect.setDate(new Date());
        int count=collectService.insert(housekeeperCollect);

        HouseKeeper houseKeeper=houseKeeperService.selectByPrimaryKeyString(houseKeeperId);


        HouseKeeperMy houseKeeperMy=new HouseKeeperMy();



        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取UUID
        houseKeeperMy.setId(getIdByUUId());
        //设置工作经验开始
        List<WorkexPerience> workexperienceList=workexPerienceService.selectByHouseKeeperId(houseKeeperId);
        int workexPerienceCount = setWorkExperienceData(workexperienceList,houseKeeperMy.getId());

        result.put("workexPerienceCount", workexPerienceCount);
        //设置培训
        List<TrainExperience>  trainExperienceList=trainExperienceService.selectByHouseKeeperId(houseKeeperId);
        int trainExperienceCount=setTrainExperienceData(trainExperienceList,houseKeeperMy.getId());
        result.put("trainExperienceCount", trainExperienceCount);
        //设置紧急联系人电话
       List<EmergencyContact> emergencyContactList= emergencyContactService.selectByHouseKeeperId(houseKeeperId);
        int emergencyContactDataCount=setEmergencyContactData(emergencyContactList,houseKeeperMy.getId());
        result.put("emergencyContactDataCount", emergencyContactDataCount);
        //设置证书
       List<Certificate> certificateList = certificateService.selectByHouseKeeperId(houseKeeperId);
        int certificateDataCount=setCertificatedataData(certificateList,houseKeeperMy.getId());
        result.put("certificateDataCount", certificateDataCount);
        houseKeeperMy.setHouseKeeperId(houseKeeperId);
        houseKeeperMy.setAdminuserid(adminuser.getId());
        houseKeeperMy.setWorkpic(houseKeeper.getWorkpic());
        houseKeeperMy.setLifepic(houseKeeper.getLifepic());
        houseKeeperMy.setName(houseKeeper.getName());
        houseKeeperMy.setHeight(houseKeeper.getHeight());
        houseKeeperMy.setWeight(houseKeeper.getWeight());
        houseKeeperMy.setProfessional(houseKeeper.getProfessional());
        houseKeeperMy.setPresentation(houseKeeper.getPresentation());
        houseKeeperMy.setPresentation2(houseKeeper.getPresentation2());
        houseKeeperMy.setHighgrade(houseKeeper.getHighgrade());
        houseKeeperMy.setExperience(houseKeeper.getExperience());
        houseKeeperMy.setSex(houseKeeper.getSex());
        houseKeeperMy.setBirthday(houseKeeper.getBirthday());

        houseKeeperMy.setZodiac(houseKeeper.getZodiac());
        houseKeeperMy.setNativeplace(houseKeeper.getNativeplace());
        houseKeeperMy.setEducation(houseKeeper.getEducation());
        houseKeeperMy.setBoardandlodging(houseKeeper.getBoardandlodging());
        houseKeeperMy.setLanguage(houseKeeper.getLanguage());
        houseKeeperMy.setCook(houseKeeper.getCook());
        houseKeeperMy.setSkill(houseKeeper.getSkill());
        houseKeeperMy.setSkill2(houseKeeper.getSkill2());
        houseKeeperMy.setWorktype(houseKeeper.getWorktype());
        houseKeeperMy.setWorktime(houseKeeper.getWorktime());
        houseKeeperMy.setWorkstate(houseKeeper.getWorkstate());
        houseKeeperMy.setSalarystart(houseKeeper.getSalarystart());
        houseKeeperMy.setSalaryend(houseKeeper.getSalaryend());
        houseKeeperMy.setWorkarea(houseKeeper.getWorkarea());
        houseKeeperMy.setPhone1(houseKeeper.getPhone1());
        houseKeeperMy.setPhone2(houseKeeper.getPhone2());
        houseKeeperMy.setCertificate(houseKeeper.getCertificate());
        houseKeeperMy.setIdcardnumber(houseKeeper.getIdcardnumber());
        houseKeeperMy.setIdcardpic(houseKeeper.getIdcardpic());
        houseKeeperMy.setJobnumber(houseKeeper.getJobnumber());



        if(count>0){
            int houseKeeperMyCount=houseKeeperMyService.addHouseKeeper(houseKeeperMy);
            result.put("msg", "ok");
            result.put("result", true);
            result.put("count", count);
            result.put("houseKeeperMyCount", houseKeeperMyCount);

        }else{
            result.put("msg", "not ok");
            result.put("result", false);
            result.put("count", 0);
            result.put("houseKeeperMyCount", 0);
        }
        System.out.println("result=="+result.toJSONString());
        return result.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/del")//收藏取消
    public String del(String houseKeeperId,String houseKeeperMyId,String state,HttpSession session){
        JSONObject result = new JSONObject();
        AdminUser adminuser=(AdminUser)session.getAttribute("adminuser");
        Map<String, Object> map = new HashMap<>();
        map.put("houseKeeperId",houseKeeperId);
        map.put("adminUserId",adminuser.getId());
        map.put("state",state);
        int count= collectService.deleteByAdminId(map);
        int houseKeeperMyCount= houseKeeperMyService.deleteByAdminId(map);
        if(houseKeeperMyId!=""&&houseKeeperMyId!=null){
            workexPerienceService.deleteByHouseKeeperId(houseKeeperMyId);
            trainExperienceService.deleteByHouseKeeperId(houseKeeperMyId);
            emergencyContactService.deleteByHouseKeeperId(houseKeeperMyId);
            certificateService.deleteByHouseKeeperId(houseKeeperMyId);
        }
        if(count>0){
            result.put("msg", "ok");
            result.put("result", true);
            result.put("count", count);
            result.put("houseKeeperMyCount", houseKeeperMyCount);
        }else{
            result.put("msg", "not ok");
            result.put("result", false);
            result.put("count", 0);
            result.put("houseKeeperMyCount", houseKeeperMyCount);
        }
        return result.toJSONString();
    }
    //设置工作经验
    public int setWorkExperienceData(List<WorkexPerience> workexperienceList, String houseKeeperMyId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int workexPerienceCount=0;
            for(int i =0;i<workexperienceList.size();i++){
                WorkexPerience workexPerience=new WorkexPerience();
                workexPerience.setId(getIdByUUId());
                workexPerience.setHousekeeperid(houseKeeperMyId);
                workexPerience.setContent(workexperienceList.get(i).getContent());
                workexPerience.setWorktimestart(workexperienceList.get(i).getWorktimestart());
                workexPerience.setWorktimeend(workexperienceList.get(i).getWorktimeend());
                int count=workexPerienceService.addWorkexPerience(workexPerience);
                if(count>0){
                    workexPerienceCount++;
                }
            }
        return workexPerienceCount;
    }
    //设置培训
    public int setTrainExperienceData( List<TrainExperience>  trainExperienceList,String houseKeeperMyId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int  trainPerienceCount=0;
            for(int i =0;i<trainExperienceList.size();i++){
                TrainExperience trainExperience=new TrainExperience();
                trainExperience.setId(getIdByUUId());
                trainExperience.setHousekeeperid(houseKeeperMyId);
                trainExperience.setTraintimeend(trainExperienceList.get(i).getTraintimeend());
                trainExperience.setTraintimestart(trainExperienceList.get(i).getTraintimestart());
                trainExperience.setCurriculumname(trainExperienceList.get(i).getCurriculumname());
                trainExperience.setTraininginstitution(trainExperienceList.get(i).getTraininginstitution());
                trainExperience.setContent(trainExperienceList.get(i).getContent());
                trainExperience.setCheckandevaluate(trainExperienceList.get(i).getCheckandevaluate());
                int count=trainExperienceService.addTrainExperience(trainExperience);
                if(count>0){
                    trainPerienceCount++;
                }
            }

        return trainPerienceCount;
    }
    //设置证书
    public int setCertificatedataData(List<Certificate> certificateList,String houseKeeperMyId){
        int certificatedataCount=0;
            for(int i =0;i<certificateList.size();i++){
                Certificate certificate=new Certificate();
                certificate.setId(getIdByUUId());
                certificate.setHousekeeperid(houseKeeperMyId);
                certificate.setCertificatename(certificateList.get(i).getCertificatename());
                certificate.setCertificatepic(certificateList.get(i).getCertificatepic());
                int count=certificateService.addCertificate(certificate);
                if(count>0){
                    certificatedataCount++;
                }
            }
        return certificatedataCount;
    }

    //设置紧急联系人
    public int setEmergencyContactData(List<EmergencyContact> emergencyContactList,String houseKeeperMyId){
        int emergencycontactdataCount=0;

            for(int i =0;i<emergencyContactList.size();i++){
                EmergencyContact emergencyContact=new EmergencyContact();
                emergencyContact.setId(getIdByUUId());
                emergencyContact.setHousekeeperid(houseKeeperMyId);
                emergencyContact.setName(emergencyContactList.get(i).getName());
                emergencyContact.setPhone(emergencyContactList.get(i).getPhone());
                int count=emergencyContactService.addEmergencyContact(emergencyContact);
                if(count>0){
                    emergencycontactdataCount++;
                }
            }

        return emergencycontactdataCount;
    }
}
